package cn.tedu.tea.service;

import cn.tedu.tea.domain.Code;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.CodeMapper;
import cn.tedu.tea.domain.param.CodeAddNewDTO;
import cn.tedu.tea.domain.param.CodeUpdateDTO;
import cn.tedu.tea.domain.vo.CodeListVO;
import cn.tedu.tea.domain.vo.CodeStandardVO;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CodeServiceImpl implements ICodeService {

    @Autowired
    private CodeMapper codeMapper;

    @Override
    public void addNew(CodeAddNewDTO codeAddNewDTO) {
        log.debug("开始处理【添加编码】的业务，参数： {}", codeAddNewDTO);
        int count = codeMapper.countByName(codeAddNewDTO.getName());
        if (count > 0){
            String message = "添加编码失败，系统名称已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Code code = new Code();
        BeanUtils.copyProperties(codeAddNewDTO, code);
        code.setModifiedUserId(0L).setModifiedUserName("");
        int rows = codeMapper.insert(code);
        if (rows != 1){
            String message = "添加编码失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除编码】的业务，参数： {}", id);
        CodeStandardVO codeStandard = codeMapper.getStandardById(id);
        if (codeStandard == null){
            String message = "删除编码失败，要删除的编码不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = codeMapper.deleteById(id);
        if (rows != 1){
            String message = "删除编码失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除编码】的业务，参数： {}", ids);
        int rows = codeMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除编码失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(CodeUpdateDTO codeUpdateDTO) {
        log.debug("开始处理【修改编码信息】的业务，参数：{}", codeUpdateDTO);
        CodeStandardVO codeStandard = codeMapper.getStandardById(codeUpdateDTO.getId());
        if (codeStandard == null){
            String message = "修改编码失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = codeMapper.countByNameAndNotId(codeUpdateDTO.getName(), codeUpdateDTO.getId());
        if (count > 0){
            String message = "修改编码失败，系统名称已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Code code = new Code();
        BeanUtils.copyProperties(codeUpdateDTO, code);
        int rows = codeMapper.update(code);
        if (rows != 1){
            String message = "修改编码失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将编码设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将编码设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public CodeStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询编码详情】的业务，参数： {}", id);
        CodeStandardVO codeStandard = codeMapper.getStandardById(id);
        if (codeStandard == null){
            String message = "查询编码详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return codeStandard;
    }

    @Override
    public List<CodeListVO> list() {
        log.debug("开始处理【查询编码列表】的业务，无参数");
        List<CodeListVO> list = codeMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        CodeStandardVO currentCode = codeMapper.getStandardById(id);
        if (currentCode == null) {
            String message = STATUS_TEXT[status] + "编码失败，编码数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentCode.getStatus() == status) {
            String message = STATUS_TEXT[status] + "编码失败，此编码已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Code updateCode = new Code();
        updateCode.setId(id);
        updateCode.setStatus(status);
        int rows = codeMapper.update(updateCode);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "编码失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
